package cn.tangwei.org.service.impl;

import cn.tangwei.basic.exception.BusinessException;
import cn.tangwei.basic.service.impl.BaseServiceImpl;
import cn.tangwei.org.domain.Employee;
import cn.tangwei.org.domain.Shop;
import cn.tangwei.org.mapper.EmployeeMapper;
import cn.tangwei.org.mapper.ShopMapper;
import cn.tangwei.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    // 基本的CRUD 分页加高级查询 批量删除通过继承BaseServiceImpl得到
    // 自己写的自定义方法不能抽取
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    @Transactional
    public void settlement(Shop shop) {
        // 一：校验，两次密码输入是否一致，不致报出业务异常
        Employee admin = shop.getAdmin();
        // 判断密码是否一致
        if(!admin.getPassword().equals(admin.getComfirmPassword())){
            throw new BusinessException("两次密码不一致");
        }else {
            // 再用户名是否已被注册
            Employee employee = employeeMapper.loadByUser(admin.getUsername());
            // 判断是否重重复
            if(employee != null){
                throw new BusinessException("该用户名已经被注册");
            }
        }
        // 入驻，保存员工信息返回自增id
        employeeMapper.save(admin);
        // 保存店铺信息返回自增id
        shop.setAdmin(admin);
        shopMapper.save(shop);
        // 修改员工所属店id
        admin.setShop_id(shop.getId());
        employeeMapper.update(admin);// 返回id


    }
}
